package com.ruoyi.project.system.controller;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysCoupon;
import com.ruoyi.project.system.service.ISysCouponService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author jjp
 * @date 2020-04-20
 */
@RestController
@RequestMapping("/system/coupon")
public class SysCouponController extends BaseController {

    @Autowired
    private ISysCouponService sysCouponService;

    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCoupon sysCoupon) {
        startPage();
        List<SysCoupon> list = sysCouponService.selectSysCouponList(sysCoupon);
        return getDataTable(list);
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:query')")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId) {
        return AjaxResult.success(sysCouponService.selectSysCouponById(couponId));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/addCoupon")
    public AjaxResult add(@RequestBody SysCoupon sysCoupon) {
        if (sysCoupon.getNeedMoney() == null) {
            sysCoupon.setNeedMoney(0.0);
        }
        return toAjax(sysCouponService.insertSysCoupon(sysCoupon));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCoupon")
    public AjaxResult edit(@RequestBody SysCoupon sysCoupon) {
        return toAjax(sysCouponService.updateSysCoupon(sysCoupon));
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds) {
        return toAjax(sysCouponService.deleteSysCouponByIds(couponIds));
    }


    /**
     * 后台维护测试 优惠券数量
     */
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping("exChangeCoupon")
    public AjaxResult exChange(Long couponId) {
        int i = sysCouponService.exchangeCoupon(couponId);
        if (i != 0) {
            return AjaxResult.success("兑换成功");
        }
        return AjaxResult.error("兑换失败");
    }
}
